<script setup>
import { ref } from 'vue'
import { onLoad } from "@dcloudio/uni-app"
import uploadFile from '@/components/upload/index.vue'
import { inType, outType } from '@/apis/dict.js'
import { uploadImgUrl } from '@/common/config.js'
import { generateUniqueId } from '@/utils/index.js'
import { confirmReceiptApi, confirmReceiptUpdateApi } from '@/apis/orderManagement.js'

const typeName = ref("")
const typeShow = ref(false)
const action = ref("")
const type = ref("inorderoperlog")
const perioDefaultValue = ref([0])
const formData = ref({
  inorderid: "", // 入库订单ID
  outorderid: "", // 出库订单ID
  company: "", // 客户名称
  opertype: "", // 操作类型
  operinfo: "", // 操作内容
  memo: "",// 备注
  id: ""// 操作ID
})

onLoad((option) => {
  type.value = option.type === 'inorder' ? 'inorderoperlog' : 'outorderoperlog'
  getInType(option.confirmType)
  const data = JSON.parse(option.data)
  formData.value.inorderid = data.id
  formData.value.outorderid = data.id
  formData.value.company = data.company
})

const typeList = ref([])
const getInType = async (currentType = '3') => {
  let res = null
  if (type.value === 'inorderoperlog') {
    res = await inType()
  } else {
    res = await outType()
  }
  if (res.data.length > 0) {
    typeList.value = res.data.map(item => {
      return {
        label: item.dictLabel,
        value: item.dictValue
      }
    })
    const item = res.data.find(item => item.dictValue === currentType)
    const index = res.data.indexOf(item)
    perioDefaultValue.value = [index]
    if (item) {
      typeName.value = item.dictLabel
      formData.value.opertype = item.dictValue
    }
  }
}

const loading = ref(false)
const buttonType = ref("default")
const clickSave = async () => {
  if (formData.value.id) {
    await confirmReceiptUpdateApi(formData.value, type.value)
    uni.$u.toast("修改成功")
    setTimeout(() => {
      uni.navigateBack()
    }, 1000)
  } else {
    buttonType.value = 'success'
    formData.value.id = generateUniqueId()
    action.value = uploadImgUrl(formData.value.id)
    await confirmReceiptApi(formData.value, type.value)
  }
}

const confirm = (e) => {
  const item = e[0]
  typeName.value = item.label
  formData.value.opertype = item.value
  const index = typeList.value.findIndex(item => item.value === formData.value.opertype)
  perioDefaultValue.value = [index]
}
</script>


<template>
  <div class="container page_container">
    <view class="content">
      <u-form :model="formData" label-width="200">
        <u-form-item label="入库订单ID:">
          <u-input v-model="formData.inorderid" disabled />
        </u-form-item>
        <u-form-item label="客户名称:">
          <u-input v-model="formData.company" disabled />
        </u-form-item>
        <u-form-item label="操作类型:">
          <u-input v-model="typeName" disabled />
        </u-form-item>
        <u-form-item label="操作内容:">
          <u-input v-model="formData.operinfo" border type="textarea" placeholder="请输入操作内容" />
        </u-form-item>
        <u-form-item label="备注:">
          <u-input v-model="formData.memo" border type="textarea" placeholder="请输入备注" />
        </u-form-item>
        <!-- 使用上传组件，仅在formData.id存在时显示 -->
        <uploadFile :id="formData.id" :action="action" v-if="formData.id" />
      </u-form>
    </view>
    <u-button @click="clickSave" :type="buttonType" :loading="loading" shape="circle">
      {{ formData.id ? '确 定' : '保 存' }}
    </u-button>
    <u-select v-model="typeShow" :default-value="[perioDefaultValue]" :list="typeList" @confirm="confirm"></u-select>
  </div>
</template>


<style scoped lang="scss">
.content {
  background: #fff !important;
  border-radius: 20rpx;
  padding: 0 40rpx;
  margin-bottom: 100rpx;
}
</style>
